package com.vpx.server.controller.system;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;

import cn.hutool.json.JSONObject;
import com.vpx.server.bean.entity.CType;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.service.system.CTypeService;
import com.vpx.server.service.system.SqlExtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "类型关系表")
@RequestMapping("/api/ctype")
public class CTypeController {
    @Resource
    private SqlExtService sqlExtService;
    @Autowired
    private CTypeService service;
    @ApiOperation("添加")
    @PostMapping("/insert")
    public ResultMsg insert(CType c){
        c.setId(IdUtil.simpleUUID());
        Date date = DateUtil.date(System.currentTimeMillis());
        c.setCreateTime(date.toString());
        service.insert(c);
        return ResultMsg.success("");
    }
    @ApiOperation("删除")
    @GetMapping("/delete")
    public ResultMsg delete(CType c){
        //删除关联表
        sqlExtService.execute("delete from c_type_rel where type_id = '"+c.getId()+"'");
        service.delete(c);
        //typeCode 1、测试用例 2、流程步  8、分支
      if("2".equals(c.getTypeCode())) {
            //删除c_template_attr表中的信息
            String sql1 = "delete from c_template_attr where template_id in (select ct.id from c_template ct where ct.type_id = '"+c.getId()+"')";
            //删除c_template表中的信息
            String sql2 = "delete from c_template ct where ct.type_id = '"+ c.getId() +"'";
            sqlExtService.execute(sql1);
            sqlExtService.execute(sql2);
        } else  {
          //用例和分支删除一样
          String sql1 = "delete from c_base_info where type_code = '"+c.getId()+"'";
          String sql2 = "delete from c_base_info_attr where type_code = '"+c.getId()+"'";
          sqlExtService.execute(sql1);
          sqlExtService.execute(sql2);
        }
        return ResultMsg.success("");
    }
    @ApiOperation("修改")
    @PostMapping("/update")
    public ResultMsg update(CType c){
        service.update(c);
        return ResultMsg.success("");
    }
    @ApiOperation("查询")
    @GetMapping("/selectAll")
    public ResultMsg selectAll(){
        List<CType> list=
                service.selectAll();
        return ResultMsg.success(list);
    }
    @ApiOperation("查询分类树")
    @GetMapping("/typeTree")
    public ResultMsg typeTree(@RequestParam @ApiParam(value = "1、测试用例 2、工具分类（流程步） 3、激励型号 4、自动判读  5、条件 6 界面操作 7 通用", name = "typeCode")String typeCode) {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setParentIdKey("parent_id");
        List<JSONObject> list = sqlExtService.findListObject("select id,type_name,type_sup_id from c_type where type_code = '"+typeCode+"' and id not in ('0da30e7d08cb47bd9b22c01705b4df0e','3ed74995dccb4239adc56520e5c33c86')", JSONObject.class);
//        List<JSONObject> list = sqlExtService.findListObject("select id,type_name,type_sup_id from c_type where type_code = '"+typeCode+"'", JSONObject.class);

        String rootId = "0";
        List<Tree<String>> treeNodes = TreeUtil.build(list, rootId, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getStr("id"));
                    tree.setName(treeNode.getStr("type_name"));
                    tree.setParentId(treeNode.getStr("type_sup_id"));
                });
        return ResultMsg.success(treeNodes);
    }
}
